package com.highcom.mapper.impl;

import com.highcom.domain.Student;
import com.highcom.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentMapperImpl implements StudentMapper {
    @Override
    public Integer update(Student student) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            //获取sqlsession工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //获取sqlsession
            sqlSession = sqlSessionFactory.openSession(true); //true 自动提交数据
            result = sqlSession.update("StudentMapper.update",student);
            //sqlSession.commit();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return result;
    }

    @Override
    public Integer delete(Integer id) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            //获取sqlsession工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //获取sqlsession
            sqlSession = sqlSessionFactory.openSession(true); //true 自动提交数据
            result = sqlSession.delete("StudentMapper.delete",id);
            //sqlSession.commit();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return result;
    }

    @Override
    public Student selectById(Integer id) {
        SqlSession sqlSession = null;
        InputStream is = null;
        Student student = null;

        try {
            //加载核心配置文件
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            //获取sqlsession工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //获取sqlsession
            sqlSession = sqlSessionFactory.openSession(true);
            student = sqlSession.selectOne("StudentMapper.selectById",id);


        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return student;
    }

    @Override
    public Integer insert(Student student) {
        Integer result = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            //获取sqlsession工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //获取sqlsession
            sqlSession = sqlSessionFactory.openSession(true); //true 自动提交数据
            result = sqlSession.insert("StudentMapper.insert",student);
            //sqlSession.commit();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return result;
    }

    @Override
    public List<Student> selectAll() {
        SqlSession sqlSession = null;
        InputStream is = null;
        List<Student> list = null;

        //加载核心配置文件
        try {
            //加载核心配置文件
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            //获取sqlsession工厂
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //获取sqlsession
            sqlSession = sqlSessionFactory.openSession(true);
            //执行sql文件中的映射语句
            list = sqlSession.selectList("StudentMapper.selectAll");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return list;
    }
}
